<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最大交换</title>
</head>
<body>
  <script>
    function fn(num) {
      let arr = num.toString().split("");
      let old = [...arr]
      let index = 0
      arr.sort((a, b) => +b - +a)
      for (let i = 0; i < arr.length; i++) {
        if (old[i] !== arr[i]) {
          index = i
          break;
        }
      }
      old[old.lastIndexOf(arr[index])] = old[index]
      old[index] = arr[index]
      return old.join('')
    }
    console.log(fn(2736))
  </script>
</body>
</html>